CREATE TABLE #temp(
	                                ID VARCHAR(50),
	                                QuestionCode VARCHAR(50) NULL,
	                                PlanNo VARCHAR(500) NULL,
                                    ERPCode VARCHAR(50) NULL
                                )
                                DECLARE @ID VARCHAR(50)
                                DECLARE @ProductCode VARCHAR(1000)
                                DECLARE @QuestionCode VARCHAR(50)
                                DECLARE @ERPCode VARCHAR(50)

								DECLARE @SQL VARCHAR(max)
								SET @SQL=''

                                DECLARE cursor_PlanNo CURSOR FOR SELECT ID,ProductCode,QuestionCode,ERPCode FROM FB_Question_Input WITH(NOLOCK) WHERE ID IN('')
                                OPEN cursor_PlanNo
                                FETCH NEXT FROM cursor_PlanNo INTO @ID,@ProductCode,@QuestionCode,@ERPCode
                                WHILE @@FETCH_STATUS=0
                                BEGIN
								    SET @SQL=@SQL+'
									INSERT INTO #temp(ID,QuestionCode,PlanNo,ERPCode)
	                                SELECT '''+@ID+''','''+@QuestionCode+''',PlanNos,'''+@ERPCode+'''
	                                FROM dbo.SplitPlanNo(dbo.GetSchecules('''+@ProductCode+'''));'
	                                FETCH NEXT FROM cursor_PlanNo INTO @ID,@ProductCode,@QuestionCode,@ERPCode
                                END
                                CLOSE cursor_PlanNo
                                DEALLOCATE cursor_PlanNo

								EXEC(@SQL)

                                DELETE FROM FB_QuestionPlan WHERE cQuestionID IN(SELECT DISTINCT ID FROM #temp);

                                INSERT INTO FB_QuestionPlan(ID,cPlanNo,cQuestionID,cQuestionNo,cCompany,cERPNo,cWBSNo)
                                SELECT NEWID(),a.PlanNo,a.ID,a.QuestionCode,
                                ISNULL(
	                                (SELECT TOP 1 cCustomer FROM [LS_DB5_load].[LeadASSYRPT].[dbo].[Rpt_SbpCustomerConfig] WITH(NOLOCK) WHERE cPlanNo=a.PlanNo),
	                                (SELECT TOP 1 cCompany FROM [LS_DB5_load].[LeadASSYRPT].[dbo].[sum_debugdata] WITH(NOLOCK) WHERE cPlanNo=a.PlanNo)
                                ),a.ERPCode,c.cWBSNo
                                FROM #temp a
                                LEFT JOIN [LS_LeadPMP_leadpmp].[LeadPMP].[dbo].[ScPlanStaticsEntry] b WITH(NOLOCK) ON a.PlanNo=b.cPlanno AND b.cIsValidate='是'
	                            LEFT JOIN [LS_LeadPMP_leadpmp].[LeadPMP].[dbo].[ScPlanStaticsEntry_Ext] c WITH(NOLOCK) ON b.dEntryID=c.dEntryID AND b.cPlanno=c.cPlanno;

                                UPDATE t2
                                SET t2.cProductLine=t1.cProdLineName,t2.ProjectManId=t1.cProjectWorkNo,t2.ProjectManName=t1.cProjectorName,t2.cPlanStage=t1.cProcStage,t2.cPlanNo=t1.PlanNo
                                FROM(
	                                SELECT a.ID,c.cProdLineName,b.cProjectWorkNo,b.cProjectorName,d.cProcStage,a.PlanNo,ROW_NUMBER() OVER(PARTITION BY a.ID ORDER BY d.cProcStage DESC) rowNumber
	                                FROM #temp a
	                                LEFT JOIN [LS_LeadPMP_leadpmp].[LeadPMP].[dbo].[ScPlanStaticsEntry] b WITH(NOLOCK) ON a.PlanNo=b.cPlanno AND b.cIsValidate='是'
	                                LEFT JOIN [LS_LeadPMP_leadpmp].[LeadPMP].[dbo].[ScPlanStaticsEntry_Ext] c WITH(NOLOCK) ON b.dEntryID=c.dEntryID AND b.cPlanno=c.cPlanno
	                                LEFT JOIN [LS_DB5_load].[LeadASSYRPT].[dbo].[sum_debugdata] d WITH(NOLOCK) ON a.PlanNo=d.cPlanNo
                                )t1
                                INNER JOIN FB_Question_Input t2 WITH(NOLOCK) ON t1.ID=t2.ID AND t1.rowNumber=1;

                                DROP TABLE #temp;

                                UPDATE q
                                SET q.LauncherDept=pss.DEPT_ORG_NAME
                                FROM FB_Question_Input q WITH(NOLOCK)
                                INNER JOIN [LS_DB8_ehr].[EHRDB].[dbo].[hr_staff_base] hsb WITH(NOLOCK) ON hsb.base_code=q.LauncherId
                                INNER JOIN [LS_DB8_ehr].[EHRDB].[dbo].[pt_staff_stru] pss WITH(NOLOCK) ON hsb.base_id=pss.staff_id AND pss.POST_STRU_ID='1'
                                WHERE q.ID IN('')